
package com.ifcar.app.util;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import java.util.Calendar;

/**
 * 定时操作工具
 * 
 * @author wjying
 */
public class AlarmUtil {
    /**
     * 设置定时
     * 
     * @param cotnext
     * @param alarmIntent
     * @param delayMillis
     */
    public static void addAlarm(Context context, PendingIntent alarmIntent, int delayMillis) {
        // PendingIntent alarmIntent = PendingIntent.getService(cotnext, 0,
        // intent,
        // PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarms.cancel(alarmIntent);

        if (delayMillis == -1) {
            // 默认5分钟
            delayMillis = 5 * 60 * 1000;
        }

        // interval /= 60;

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.MILLISECOND, delayMillis);

        alarms.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
    }

    /**
     * 删除定时
     * 
     * @param cotnext
     * @param alarmIntent
     */
    public static void deleteAlarm(Context context, PendingIntent alarmIntent) {
        // PendingIntent alarmIntent = PendingIntent.getService(cotnext, 0,
        // intent,
        // PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarms.cancel(alarmIntent);
    }

}
